import http from '@/config/config.js';
// let BASE_URL = 'http://pbb.zhonghengqi.com/api/'
let token = ''; //凭证
// let data = {
// 	tokens: uni.getStorageSync('token')
// }
function request(options = {}) {
	options.url = `${http.BASE_URL}${options.url}`;
	//配置请求头
	// options.header = {
	// 	'content-type': 'application/x-www-form-urlencoded',
	// 	'token': uni.getStorageSync('userinfo').token //Bearer 
	// };
	options.header = Object.assign({
		'content-type': 'application/x-www-form-urlencoded',
		'token': uni.getStorageSync('userinfo').token //Bearer 
	},options.header?options.header:{});
	return new Promise((resolve, reject) => {
			//成功
			options.success = (res) => {
				// console.log(res,'请求')
				if(res.statusCode === 500){
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 1000
					})
				}
				if(res.data.code !== 1){
					if(res.data.code === 401){
						// uni.setStorageSync('pages',pages)
						let pages = getCurrentPages();
						
						uni.removeStorageSync('userinfo')
						uni.showToast({
							title: res.data.msg,
							icon: 'none',
							duration: 1000
						});
						uni.navigateTo({
							url: '/pages/login/login?pages=' +pages[pages.length-1].route,
							complete: e=>{
							}
						});
					}else{
						uni.showToast({
							title: res.data.msg,
							icon: 'none',
							duration: 2000
						});
						// reject(res.msg)
					}
					resolve(res.data)
				}
				resolve(res.data)
			}
			//错误

			options.fail = (err) => {
				// #ifdef MP-ALIPAY
				if(err.data.code == 401 && err.statusCode == 401){
					uni.removeStorageSync('userinfo');
					uni.showToast({
						title: err.data.msg,
						icon: 'none'
					});
					uni.navigateTo({
						url: '/pages/login/login',
						complete: e=>{
						}
					});
					return;
				};
				// #endif
				reject(err); //错误
				uni.showToast({
					title: '系统异常',
					icon: 'none'
				});
			};
			 // options.data ={token: uni.getStorageSync('token'),...options.data}
			 // options.data = data;
			uni.request(options);
		})
}
export default request;
